package defaultPackage;
import java.awt.*;
import taskMgr.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import console.console;;

@SuppressWarnings("serial")
public class MainClass extends Frame implements Runnable, WindowListener
{
	static boolean paused = false;
    Thread thread;
    long startTime, endTime, framePeriod;
    Dimension dim;
    BufferedImage buffer;
    Graphics g;
    public Grid grid; 
    public Cursor cursor;
    public DesignateMenu dmenu;
    public PeopleManager people;
    ConstructionMenu cmenu;
    Designation designation;
    public Camera camera;
    Background background;
    KeyMenu keymenu;
    CursorWindow cwindow;
    public ObjectManager objectmanager;
    EventHandler eventhandler;
    KeyListener input;
    public console com;
	static int windowXSize = 1280,windowYSize = 760, menuHeight = 200;
	AnnouncementMenu amenu;
	HelpMenu hmenu;
	CursorMenu cumenu;
	Manager taskManager;
	static final Font defaultFont = new Font("Dialog",Font.PLAIN,12);
    
    
    public static void main (String args[])
    {
        MainClass f = new MainClass ();
        f.setSize(windowXSize,windowYSize);
        f.setVisible (true);
        f.setLayout (new FlowLayout());     
    }
    
	public MainClass()
    {
        this.addWindowListener(this);
        startTime = 0;
        endTime = 0;
        framePeriod = 25;
        dim = getSize();
        thread = new Thread (this);
        thread.start();
        ObjectManager.numObjects = 4;
        grid = new Grid(this);
        objectmanager = new ObjectManager(grid,this);
        grid.setObjectmanager(objectmanager);
        camera = new Camera(Grid.dimensionx*10,Grid.dimensiony*10);
        people = new PeopleManager(this);
        cursor = new Cursor(this,camera);
        cmenu = new ConstructionMenu(this);
        designation = new Designation();
        dmenu = new DesignateMenu(designation);
        Resources.setResources(1000);
        background = new Background();
        paused = false;
        keymenu = new KeyMenu();
        cwindow = new CursorWindow(cursor,grid.tiles[cursor.x/10][cursor.y/10],objectmanager.objects,this);
        buffer = new BufferedImage(windowXSize, windowYSize, BufferedImage.TYPE_INT_ARGB);
        eventhandler = new EventHandler();
        input = new Input(this);
        com = new console(this);
        com.start();
        addKeyListener(this.input);
        amenu = new AnnouncementMenu();
        hmenu = new HelpMenu();
        cumenu = new CursorMenu();
        taskManager = new Manager();
        
        for (int a = 30; a<=51;a++)
        {
        	for (int b = 30; b<= 51;b++)
        		grid.tiles[a][b].changeType(tileType.Hull);
        }
        for (int a = 31; a<=50;a++)
        {
        	for (int b = 31; b<= 50;b++)
        	{
        		grid.tiles[a][b].changeType(tileType.Interior);
        		grid.tiles[a][b].setOxygen(100);
        	}
        }
        for (int b = 32; b <=50;b++)
        	grid.tiles[33][b].changeType(tileType.Hull);
        
        objectmanager.objectInit();
        
        
        people.addPerson(new Person(this,34,34,"Bob",CrewType.Captain));
		people.getPerson("Bob").setmap();
		
		people.addPerson(new Person(this,35 ,34,"Steve", CrewType.Crewman));
		people.getPerson("Steve").setmap();
		//people.getPerson("Steve").setWearingSpaceSuit(true);
    }
    
    
    public void run()
    {
        for (;;)
        {
            startTime = System.currentTimeMillis();
            repaint ();
            try
            {
                endTime = System.currentTimeMillis();
                if (framePeriod- (endTime - startTime)>0)
                    Thread.sleep (framePeriod - (endTime - startTime));
                }
                catch (InterruptedException e)
                {
                }
            }  
        }
    public void paint (Graphics finalbuffer)
    {
    	Graphics gfx = buffer.getGraphics();
    	gfx.setFont(defaultFont);
    	background.draw(camera.g);
        grid.draw(camera.g);
        for (int i = 0; i < ObjectManager.numObjects; i++)
            objectmanager.objects[i].draw(camera.g);
        taskManager.draw(camera.g);
        designation.draw(camera.g);
        cursor.draw(camera.g);
        people.draw(camera.g);
        camera.draw(gfx);
        gfx.setColor(Color.LIGHT_GRAY);
        gfx.fillRect(windowXSize - 200, 0, 200, windowYSize);
        if (!dmenu.isActive() && !cmenu.isActive())
        	keymenu.draw(gfx);
        dmenu.draw(gfx);
        cmenu.draw(gfx);
        cwindow.draw(gfx);
        amenu.draw(gfx);
        cumenu.draw(gfx);
        
        if (paused)
        {
        	Font pausedFont  = new Font("Dialog",Font.PLAIN,20);
        	gfx.setFont(pausedFont);
        	gfx.setColor(Color.red);
        	gfx.drawString("PAUSED", windowXSize - 300, 45);
        	gfx.setFont(defaultFont);
        }
        Resources.draw(gfx);
        hmenu.draw(gfx);
        
        finalbuffer.drawImage(buffer, 0, 0, null);
    }    
    
    public void update (Graphics gfx)
    {
    	if (!paused)
    	{
    		objectmanager.updateObjects();
    		grid.updateOxygen(objectmanager.objects);
    		eventhandler.update(this);
    		people.update();
    	}
    	cwindow = new CursorWindow(cursor,grid.tiles[cursor.x/10][cursor.y/10],objectmanager.objects,this);
    	designation.moveDesignation(cursor);
        paint (gfx);
        Resources.redCount();
    }

    
    
    
    
    public void windowClosing (WindowEvent e)
    {
        dispose ();
        System.exit(0);
    }
    
    public void windowOpened (WindowEvent e)
    {
        
    }
    
    public void windowIconified (WindowEvent e)
    {
        
    }
    
    public void windowClosed (WindowEvent e)
    {
        
    }
    
    public void windowDeiconified (WindowEvent e)
    {
        
    }
    
    public void windowActivated (WindowEvent e)
    {
        
    }
    
    public void windowDeactivated (WindowEvent e)
    {
        
    }
}
